<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

<script>
    {
        const list = new Map()
        for (let i = 0; i < 100; i++) {
            list.set(i, `http://localhost:8081?i=${i}`)
        }
        console.log('有10000个数据，需要批量提交到服务器', list.size)
        // for (let i = 0; i < 10; i++) {
        //     axios.post('http://localhost:8081',{i: list.get(i)})
        // }
        concurRequest(list,5).then(async res => {
            console.log('done',res)
        })
        function concurRequest(urls, maxNum) {
            return new Promise((resolve) => {
                let index = 0  //下一次请求的下标
                const result = []  //储存所有请求结果
                //let count = 0  //当前完成的请求数量
                async function _request() {
                    const i = index
                    const url = urls.get(index)
                    index++
                    try {
                        const res = await fetch(url)
                        result[i] = res
                    } catch (err) {
                        result[i] = err
                    } finally {
                        //count++
                        if (result.length === urls.size) {
                            resolve(result)
                        } else if (index < urls.size) {
                            _request()
                        }
                    }
                }
                for (let i = 0; i < Math.min(urls.size, maxNum); i++) {
                  _request()
                }
            })
        }
    }
    {

    }
</script>
</body>
</html>
